package com.medicine.hbp.fragment.x;

import java.util.List;

import org.kymjs.kjframe.http.HttpParams;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.medicine.hbp.adapter.FragmentAdapter01;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.UserContributeVo;
import com.medicine.hbp.mob.artGroupVo;
import com.medicine.hbp.mob.laberInfoVo;
import com.medicine.hbp.ui.ClassificationDetailActivity;
import com.medicine.hbp.uitl.CommUtil;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.xListView.XListView;
import com.medicine.hbp.xListView.XListView.IXListViewListener;
import com.medicine.hbp.R;

public class Fragment05  extends Fragment implements OnItemClickListener, IXListViewListener {
	private View View;
	private XListView xlistview;
	private FragmentAdapter01 adapter;

	private volatile boolean refresh = true;
	private volatile int page = 1;// 当前页（从0开始）
	private int pageSize = 10;

	private List<artGroupVo> artGroupVos;
//	private List<laberInfoVo> laberInfoVos;

	@Override
	public android.view.View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View = inflater.inflate(R.layout.fragment_02, container, false);
		xlistview = (XListView) View.findViewById(R.id.xListView);
		initView();
		return View;
	}
	private void initView() {
		adapter = new FragmentAdapter01(getActivity());
		xlistview.setAdapter(adapter);
		xlistview.setOnItemClickListener(this);
		xlistview.setXListViewListener(this);
		httpLaberGroupList();
	}

	/**
	 * 首页标签组获取
	 */
	private void httpLaberGroupList() {
		HttpParams params = new HttpParams();
		params.put("current", page);// 当前页数(不传默认为1)
		params.put("rowCount", pageSize);// 每页显示数(不传默认为10)
		AppHttps.HttpPost(getActivity(),HttpInterface.USER_COLLECT_LIST, params,
				new AppHttpCallBack<UserContributeVo>(UserContributeVo.class,
						getActivity()) {
					@Override
					public void onSuccess(UserContributeVo result) {
						boolean hasMore = true;
						if (refresh) {
							adapter.changeData(result.artInfo);
						} else {
							adapter.addItems(result.artInfo);
							adapter.notifyDataSetChanged();
						}
						if (result.artInfo.size() < pageSize) {
							hasMore = false;
						}
						xlistview.setPullLoadEnable(hasMore);
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(getActivity(), errorMsg);
					}

					@Override
					public void onFinish() {
						super.onFinish();
						xlistview.setRefreshTime(CommUtil.formatDate(System
								.currentTimeMillis()));
					}
				});

	}
	@Override
	public void onItemClick(AdapterView<?> arg0, android.view.View arg1, int arg2, long arg3) {
		ToastUtil.showShort(getActivity(), arg2 + "");
		List<laberInfoVo> laberInfoVos=adapter.getItems();
		Intent intent = new Intent(getActivity(), ClassificationDetailActivity.class);
		intent.putExtra("ID", laberInfoVos.get(arg2 - 2).laberId);
		intent.putExtra("NAME", laberInfoVos.get(arg2 - 2).laberName);
		intent.putExtra("IMG", laberInfoVos.get(arg2 - 2).laberImg);
		getActivity().startActivity(intent);

	}

	@Override
	public void onRefresh() {
		page=1;
		refresh = true;
		xlistview.setPullLoadEnable(false);
		httpLaberGroupList();
	}

	@Override
	public void onLoadMore() {
		page++;
		refresh = false;
		httpLaberGroupList();
	}
}